<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="账号" prop="numid">
                <el-input v-model="queryParams.numid" placeholder="请输入账号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="用户名" prop="username">
                <el-input v-model="queryParams.username" placeholder="请输入用户名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input v-model="queryParams.email" placeholder="请输入邮箱" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="手机号" prop="phoneNumber">
                <el-input v-model="queryParams.phoneNumber" placeholder="请输入手机号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="头像" prop="headPortrait">
                <el-input v-model="queryParams.headPortrait" placeholder="请输入头像" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="身份证" prop="idCard">
                <el-input v-model="queryParams.idCard" placeholder="请输入身份证" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="出生日期" prop="birthday">
                <el-input v-model="queryParams.birthday" placeholder="请输入出生日期" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="籍贯" prop="nativePlace">
                <el-input v-model="queryParams.nativePlace" placeholder="请输入籍贯" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="民族" prop="nation">
                <el-input v-model="queryParams.nation" placeholder="请输入民族" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="入党日期" prop="joinPartyDate">
                <el-date-picker clearable v-model="queryParams.joinPartyDate" type="date" value-format="YYYY-MM-DD"
                    placeholder="请选择入党日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="入职日期" prop="joinWorkDate">
                <el-date-picker clearable v-model="queryParams.joinWorkDate" type="date" value-format="YYYY-MM-DD"
                    placeholder="请选择入职日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="创建日期" prop="createdAt">
                <el-date-picker clearable v-model="queryParams.createdAt" type="date" value-format="YYYY-MM-DD"
                    placeholder="请选择创建日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="更新日期" prop="updatedAt">
                <el-date-picker clearable v-model="queryParams.updatedAt" type="date" value-format="YYYY-MM-DD"
                    placeholder="请选择更新日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasPermi="['personal:user:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['personal:user:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['personal:user:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                    v-hasPermi="['personal:user:export']">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="${comment}" align="center" prop="id" />
            <el-table-column label="账号" align="center" prop="numid" />
            <el-table-column label="用户名" align="center" prop="username" />
            <el-table-column label="邮箱" align="center" prop="email" />
            <el-table-column label="手机号" align="center" prop="phoneNumber" />
            <el-table-column label="头像" align="center" prop="headPortrait" />
            <el-table-column label="身份证" align="center" prop="idCard" />
            <el-table-column label="性别" align="center" prop="gender" />
            <el-table-column label="出生日期" align="center" prop="birthday" />
            <el-table-column label="等级" align="center" prop="grade" />
            <el-table-column label="人员分类" align="center" prop="personnelClassification" />
            <el-table-column label="正式编制" align="center" prop="formallyEstablished" />
            <el-table-column label="部门名称" align="center" prop="departmentName" />
            <el-table-column label="文化程度" align="center" prop="degreeEducation" />
            <el-table-column label="主职务" align="center" prop="mainDuties" />
            <el-table-column label="籍贯" align="center" prop="nativePlace" />
            <el-table-column label="民族" align="center" prop="nation" />
            <el-table-column label="入党日期" align="center" prop="joinPartyDate" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.joinPartyDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="入职日期" align="center" prop="joinWorkDate" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.joinWorkDate, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="政治面貌" align="center" prop="politicalOutlook" />
            <el-table-column label="婚姻" align="center" prop="marriage" />
            <el-table-column label="创建日期" align="center" prop="createdAt" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="更新日期" align="center" prop="updatedAt" width="180">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['personal:user:edit']">修改</el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['personal:user:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改个人信息管理对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="userRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="账号" prop="numid">
                    <el-input v-model="form.numid" placeholder="请输入账号" />
                </el-form-item>
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="form.username" placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="form.email" placeholder="请输入邮箱" />
                </el-form-item>
                <el-form-item label="手机号" prop="phoneNumber">
                    <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="头像" prop="headPortrait">
                    <el-input v-model="form.headPortrait" placeholder="请输入头像" />
                </el-form-item>
                <el-form-item label="身份证" prop="idCard">
                    <el-input v-model="form.idCard" placeholder="请输入身份证" />
                </el-form-item>
                <el-form-item label="出生日期" prop="birthday">
                    <el-input v-model="form.birthday" placeholder="请输入出生日期" />
                </el-form-item>
                <el-form-item label="籍贯" prop="nativePlace">
                    <el-input v-model="form.nativePlace" placeholder="请输入籍贯" />
                </el-form-item>
                <el-form-item label="民族" prop="nation">
                    <el-input v-model="form.nation" placeholder="请输入民族" />
                </el-form-item>
                <el-form-item label="入党日期" prop="joinPartyDate">
                    <el-date-picker clearable v-model="form.joinPartyDate" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择入党日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="入职日期" prop="joinWorkDate">
                    <el-date-picker clearable v-model="form.joinWorkDate" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择入职日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="创建日期" prop="createdAt">
                    <el-date-picker clearable v-model="form.createdAt" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择创建日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="更新日期" prop="updatedAt">
                    <el-date-picker clearable v-model="form.updatedAt" type="date" value-format="YYYY-MM-DD"
                        placeholder="请选择更新日期">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>




        
    </div>
</template>

<script setup name="User">
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/personal/user";

const { proxy } = getCurrentInstance();

const userList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        numid: null,
        username: null,
        email: null,
        phoneNumber: null,
        headPortrait: null,
        idCard: null,
        gender: null,
        birthday: null,
        grade: null,
        personnelClassification: null,
        formallyEstablished: null,
        departmentName: null,
        degreeEducation: null,
        mainDuties: null,
        nativePlace: null,
        nation: null,
        joinPartyDate: null,
        joinWorkDate: null,
        politicalOutlook: null,
        marriage: null,
        createdAt: null,
        updatedAt: null
    },
    rules: {
        numid: [
            { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        username: [
            { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        email: [
            { required: true, message: "邮箱不能为空", trigger: "blur" }
        ],
        idCard: [
            { required: true, message: "身份证不能为空", trigger: "blur" }
        ],
        gender: [
            { required: true, message: "性别不能为空", trigger: "change" }
        ],
        birthday: [
            { required: true, message: "出生日期不能为空", trigger: "blur" }
        ],
        createdAt: [
            { required: true, message: "创建日期不能为空", trigger: "blur" }
        ],
        updatedAt: [
            { required: true, message: "更新日期不能为空", trigger: "blur" }
        ]
    }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询个人信息管理列表 */
function getList() {
    loading.value = true;
    listUser(queryParams.value).then(response => {
        userList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        numid: null,
        username: null,
        email: null,
        phoneNumber: null,
        headPortrait: null,
        idCard: null,
        gender: null,
        birthday: null,
        grade: null,
        personnelClassification: null,
        formallyEstablished: null,
        departmentName: null,
        degreeEducation: null,
        mainDuties: null,
        nativePlace: null,
        nation: null,
        joinPartyDate: null,
        joinWorkDate: null,
        politicalOutlook: null,
        marriage: null,
        createdAt: null,
        updatedAt: null
    };
    proxy.resetForm("userRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加个人信息管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _id = row.id || ids.value
    getUser(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改个人信息管理";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["userRef"].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateUser(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addUser(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal.confirm('是否确认删除个人信息管理编号为"' + _ids + '"的数据项？').then(function () {
        return delUser(_ids);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('personal/user/export', {
        ...queryParams.value
    }, `user_${new Date().getTime()}.xlsx`)
}

getList();
</script>
